﻿<%@ WebHandler Language="C#" Class="shezhi" %>

using System;
using System.Web;
using System.Data;
using KeaoBase;
using System.Configuration;
using ServiceStack.Redis;

public class shezhi : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        try
        {
            string uid = context.Request.QueryString["uid"];
            string kid = context.Request.QueryString["kid"];
            if (!Auth.UserLogined(uid, kid))
            {
                context.Response.Write("登录信息失效，请重新登陆后重试...");
                return;
            }
            string key = KeaoUtils.GetMD5Str(context.Request.Url.OriginalString.Replace(uid,"cache").Replace(kid,"cache"));
            using (IRedisClient redis = RedisClientPool.GetReadOnlyRedisClient())
            {
                if (redis != null)
                {
                    if (redis.ContainsKey(key))
                    {
                        context.Response.Write(redis.GetValue(key));
                        return;
                    }
                }
            }
            BaseDataBase m_DB = System.Reflection.Assembly.Load("KeaoDataBase").CreateInstance(string.Format("KeaoDataBase.{0}", ConfigurationManager.AppSettings["数据库类型"])) as BaseDataBase;
            if (!m_DB.InitDataBase(ConfigurationManager.ConnectionStrings["DataSource"].ConnectionString))
            {
                context.Response.Write("请重试...");
                return;
            }
            else
            {
                string yueJieRiQi = context.Request.QueryString["yuejieriqi"];
                if (!string.IsNullOrEmpty(yueJieRiQi))
                {
                    //Configuration webConfig = WebConfigurationManager.OpenWebConfiguration(context.Request.ApplicationPath);
                    //AppSettingsSection appSet = (AppSettingsSection)webConfig.GetSection("appSettings");
                    //appSet.Settings.Remove("月结");
                    //appSet.Settings.Add("月结", yueJieRiQi);
                    //webConfig.Save();
                    string createCondition = m_DB.CreateAndCondition("fenzu", DataValueCommonType.字符串, "月结", "", SearchConditionType.等于);
                    DataTable Dt = m_DB.GetDataTableByCondition("tbl_zidian", createCondition);
                    TableRow editRow = new TableRow("tbl_zidian", string.Format("{0}", Dt.Rows[0]["id"]), DBCtl.修改);
                    TableCol col1 = new TableCol() { ColName="xiangmu", ValueMember=yueJieRiQi, ValueType= DataValueCommonType.字符串 };
                    editRow.DataCols.Add(col1);
                    m_DB.SaveTableRows(editRow);
                    using (IRedisClient redis = RedisClientPool.GetRedisClient())
                    {
                        if (redis != null&&!redis.HadExceptions)
                        {
                            redis.SetEntryInHash(uid, "yuejie", yueJieRiQi);
                        }
                    }
                    context.Response.Write("OK");
                }
                else
                {
                    string yueJieStr = string.Empty;
                    using (IRedisClient redis = RedisClientPool.GetReadOnlyRedisClient())
                    {
                        if (redis != null)
                        {
                            yueJieStr = redis.GetValueFromHash(uid, "yuejie");
                        }
                    }
                    if (string.IsNullOrEmpty(yueJieStr))
                    {
                        string createCondition = m_DB.CreateAndCondition("fenzu", DataValueCommonType.字符串, "月结", "", SearchConditionType.等于);
                        DataTable Dt = m_DB.GetDataTableByCondition("tbl_zidian", createCondition);
                        yueJieStr = string.Format("{0}", Dt.Rows[0]["xiangmu"]);
                    }
                    context.Response.Write(yueJieStr);
                }
            }
        }
        catch(Exception err)
        {
            KeaoUtils.LogTxt(string.Format("设置月结信息异常。{0}", err.Message));
            context.Response.Write("请重试...");
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}